package com.stenio.fancat.service.login;

import java.util.*;

import com.stenio.fancat.common.errorCode.ErrorCode;
import com.stenio.fancat.domain.baby.BabyRelationship;
import com.stenio.fancat.domain.user.UserRelationship;
import com.stenio.fancat.domain.user.UserRelationshipCandy;
import com.stenio.fancat.domain.user.UserRelationshipLog;
import com.stenio.fancat.mapper.baby.BabyAccountMapper;
import com.stenio.fancat.mapper.baby.BabyIconImageMapper;
import com.stenio.fancat.mapper.baby.BabyRelationshipMapper;
import com.stenio.fancat.mapper.user.UserRelationshipCandyMapper;
import com.stenio.fancat.mapper.user.UserRelationshipLogMapper;
import com.stenio.fancat.mapper.user.UserRelationshipMapper;
import com.stenio.fancat.mapper.user.UserRelationshipNameMapper;
import com.stenio.fancat.util.*;
import com.stenio.fancat.vo.BabyAccountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.stenio.fancat.exception.FanCatException;
import com.stenio.fancat.form.LoginForm;

/**
 * 登录模块服务实现类
 *
 */
@Service
public class LoginService {

	@Autowired
	private StringRedisTemplate redis;

	@Autowired
	private BabyRelationshipMapper babyRelationshipMapper;

	@Autowired
	private UserRelationshipMapper userRelationshipMapper;

	@Autowired
	private UserRelationshipNameMapper userRelationshipNameMapper;

	@Autowired
	private BabyIconImageMapper babyIconImageMapper;

	@Autowired
	private BabyAccountMapper babyAccountMapper;

	@Autowired
	private UserRelationshipCandyMapper userRelationshipCandyMapper;

	@Autowired
	private UserRelationshipLogMapper userRelationshipLogMapper;


	/**
	 * 登录方法
	 * @param form
	 * @return
	 * @throws FanCatException
	 */
	@Transactional
	public Map<String,Object> login(LoginForm form) throws FanCatException {
		if (EmptyUtil.isEmpty(form)) {
			throw new FanCatException("参数异常", ErrorCode.loginError);
		}

		Map<String,Object> map = new HashMap<>();

		UserRelationship userRelationship = getUser(form.getUnionId());  //数据库查询是否存在此用户

		if(EmptyUtil.isEmpty(userRelationship)){
			userRelationship = new UserRelationship();   //新用户，添加至数据库中
			userRelationship.setUserId(StringUtil.getUUID());
			userRelationship.setUnionId(form.getUnionId());
			userRelationship.setPhone(form.getPhone());
			userRelationship.setName(form.getName());
			userRelationship.setOpenId(form.getOpenId());
			userRelationship.setIcon(form.getIcon());
			Date date = new Date();
			userRelationship.setCreateTime(date);
			userRelationship.setUpdateTime(date);
			userRelationship.setState(0);
			userRelationshipMapper.insert(userRelationship);//新用户，添加至数据库中

		}else {
			//实时更新微信的头像和昵称
			userRelationshipMapper.modifyInfo(form.getName(),form.getIcon(),form.getUnionId());
		}

		List<BabyRelationship> list = babyRelationshipMapper.selectBabyByUid(userRelationship.getUserId());
		if(EmptyUtil.isNotEmpty(list)){
			List<BabyAccountVO> babyAccountVOList = new ArrayList<>();
			for(BabyRelationship baby : list){
				BabyAccountVO babyAccountVO = new BabyAccountVO();
				babyAccountVO.setBabyId(baby.getBabyId());
				String nickName = babyAccountMapper.selectByBid(baby.getBabyId()).getNickName();
				babyAccountVO.setNickName(nickName);
				String babyIcon = babyIconImageMapper.selectByBid(baby.getBabyId()).getImage();
				babyAccountVO.setImage(babyIcon);
				Integer relationship = babyRelationshipMapper.selectBabyByUidAndBid(userRelationship.getUserId(),baby.getBabyId()).getRelationship();
				babyAccountVO.setRelationship(relationship);
				String relationshipName = userRelationshipNameMapper.selectName(relationship).getRelationshipName();
				babyAccountVO.setRelationshipName(relationshipName);
				babyAccountVOList.add(babyAccountVO);
				map.put("babyList",babyAccountVOList);
				if (baby.getRelationship() == 1 || baby.getRelationship() == 2){
					map.put("showBabyId",baby.getBabyId());
					map.put("showUserId",userRelationship.getUserId());
					return map;
				}
			}
			map.put("showBabyId",list.get(0).getBabyId());
			map.put("showUserId",userRelationship.getUserId());

		}else{
			map.put("showUserId",userRelationship.getUserId());
		}

		return map;
	}



	/**
	 * 通过unionId查询用户
	 * @param unionId
	 * @return
	 */
	public UserRelationship getUser(String unionId) {
		return userRelationshipMapper.selectUserInfoByUId(unionId);
	}


	/**
	 * 根据key键删除缓存（用户对象）
	 * @param token
	 */
	public void delete(String token){
		redis.delete(token);
	}

}
